package com.hmsg.health.controller.admin;


import com.hmsg.health.bean.Dto.AppealDto;
import com.hmsg.health.bean.Dto.AppealDtos;
import com.hmsg.health.bean.Dto.UmengExtra;
import com.hmsg.health.bean.domain.AppealDo;
import com.hmsg.health.bean.domain.TaskTypeDo;
import com.hmsg.health.bean.domain.UserDo;
import com.hmsg.health.bean.enums.StatusCode;
import com.hmsg.health.bean.vo.AppealReqVo;
import com.hmsg.health.bean.vo.TaskSecondReqVo;
import com.hmsg.health.dao.AppealDao;
import com.hmsg.health.dao.UserDao;
import com.hmsg.health.param.ResponseResult;
import com.hmsg.health.security.IgnoreSecurity;
import com.hmsg.health.security.OpenApi;
import com.hmsg.health.service.admin.AdminAppealservice;
import com.hmsg.health.utils.umeng.push.Demo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;

import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Api(value = "运营系统用户异常申请相关", tags = { "运营系统用户异常申请相关" })
@Slf4j
@Controller
@RequestMapping("/taskUnusua")
public class AdminUnusualController {

    @Autowired
    private AdminAppealservice adminAppealservice;
    @Autowired
    private AppealDao appealDao;
    @Autowired
    private UserDao userDao;

    @GetMapping("/list")
    @ApiOperation(value="异常审核列表", notes="异常审核列表" ,httpMethod="GET")
    @ResponseBody
    @IgnoreSecurity
    public ResponseResult<AppealDtos> getList( @RequestParam(value = "typeId",required = false) Integer typeId,
                                               @RequestParam(value = "phone",required = false) String phone,
                                               @RequestParam(value = "time",required = false) String time,
                                               @RequestParam(value = "pageNo") Integer pageNo,
                                          @RequestParam(value = "pageSize") Integer pageSize) {

        Integer firstResult = (pageNo-1)*pageSize;
        Integer maxResult =pageSize;

        int totalpages;//总页数
        int total=appealDao.findCount(typeId,phone,time);
        totalpages=   total/pageSize;
        if(total% pageSize !=0){
            totalpages++;
        }



        List<AppealDto>  appealDtos=adminAppealservice.findAll(typeId,phone,time,firstResult,maxResult);
        AppealDtos appealDtos1=new AppealDtos();
        appealDtos1.setAppealDtoList(appealDtos);
        appealDtos1.setTotalpages(totalpages);
        return new ResponseResult<AppealDtos>(StatusCode.SYSTEM_SUCCESS, appealDtos1);
    }

    @PostMapping("/verify")
    @ApiOperation(value="审核", notes="审核" ,httpMethod="POST")
    @ResponseBody
    @IgnoreSecurity
    @OpenApi
    public ResponseResult<Object> editSecond( @ApiParam(name = "审核", value = "传入json格式", required = true)
                                              @RequestBody @Validated AppealReqVo vo) throws Exception {
        AppealDo appealDo=new AppealDo();
        appealDo.setId(vo.getAppealId());
        appealDo.setVerifyReason(vo.getAppealReason());
        appealDo.setStatus(vo.getIsState());
        adminAppealservice.update(appealDo);
        Demo submitMsg = new Demo();
        AppealDo result = appealDao.findOne(vo.getAppealId());
        if(result!=null) {
        	Integer userId = result.getUserId();
        	UserDo userInfo = userDao.findById(userId);
        	String deviceToken = userInfo.getDeviceToken();

    		
    		String body = vo.getIsState()==1?"审核通过，金币将打入您的钱包，请注意查收":"审核不通过，具体信息请查看任务详情";
    		List<UmengExtra> list = new ArrayList<UmengExtra>();
    		UmengExtra ue1 = new UmengExtra();
    		ue1.setKey("type");
    		ue1.setValue(3);
    		list.add(ue1);
    		
    		UmengExtra ue2 = new UmengExtra();
    		ue2.setKey("resourceId");
    		ue2.setValue(vo.getAppealId());
    		list.add(ue2);
        	if(deviceToken.length()>44) { // ios
        		JSONObject params = new JSONObject();
        		params.put("title", "异常审核通知");
        		params.put("body", body);
        		submitMsg.sendIOSUnicast(deviceToken, params, list);
        	}else { // android
        		submitMsg.sendAndroidUnicast(deviceToken, "异常审核通知", "异常审核通知", body, list);
        	}
        	
        }
        return new ResponseResult<Object>(StatusCode.SYSTEM_SUCCESS, null);
    }


}

